package com.dongbo.recorder.screenrecord.utils;

import android.app.Activity;
import android.app.Dialog;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.dongbo.recorder.screenrecord.ui.activity.WebActivity;
import com.dongbo.recorder.screenrecord.Constants;
import com.dongbo.recorder.screenrecord.R;

public class DialogUtils {

    public static Dialog initDialog(Activity activity, LoginDialog loginDialog) {
        Dialog dialog = new Dialog(activity, R.style.custom_dialog);
        dialog.setContentView(R.layout.layout_vip_remind_dialog);
        dialog.setCancelable(false);
        TextView tvContent = dialog.findViewById(R.id.tvContent);
        TextView tvCancel = dialog.findViewById(R.id.tvCancel);
        TextView tvYes = dialog.findViewById(R.id.tvYes);


        ClickableSpan clickableSpan1 = new ClickableSpan() {

            @Override
            public void onClick(@NonNull View view) {
                WebActivity.start(activity,
                        "用户协议",
                        Constants.BASE_URL + "app/article/u/protocolInfo?protocolId=f9da6036f2294ea9824830d0601b4eda");
//                WebActivity.start(activity, "用户协议", Constants.FUWU);
            }
        };

        ClickableSpan clickableSpan2 = new ClickableSpan() {

            @Override
            public void onClick(@NonNull View view) {
                WebActivity.start(activity, "隐私政策",Constants.BASE_URL + "app/article/u/protocolInfo?protocolId=c099b1915f2d4a0daaf7781578da0e17");
            }
        };

        SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
        stringBuilder.append("我们很注重用户隐私的保护，请阅读并同意用户注册协议和隐私政策，我们会竭诚保护您的权益。");
//            ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(activity.getResources().getColor(R.color.colorRed2));
        String privacyTitle1 = "用户注册协议";
        String privacyTitle2 = "隐私政策";
//            stringBuilder.setSpan(foregroundColorSpan, stringBuilder.toString().indexOf(privacyTitle1), stringBuilder.toString().indexOf(privacyTitle1) + privacyTitle1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        stringBuilder.setSpan(clickableSpan1, stringBuilder.toString().indexOf(privacyTitle1), stringBuilder.toString().indexOf(privacyTitle1) + privacyTitle1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        stringBuilder.setSpan(new UnderlineSpan() {
            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(activity.getResources().getColor(R.color.color_main));
                ds.setUnderlineText(false);
            }
        }, stringBuilder.toString().indexOf(privacyTitle1), stringBuilder.toString().indexOf(privacyTitle1) + privacyTitle1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);


//            stringBuilder.setSpan(foregroundColorSpan, stringBuilder.toString().indexOf(privacyTitle2), stringBuilder.toString().indexOf(privacyTitle2) + privacyTitle2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        stringBuilder.setSpan(clickableSpan2, stringBuilder.toString().indexOf(privacyTitle2), stringBuilder.toString().indexOf(privacyTitle2) + privacyTitle2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        stringBuilder.setSpan(new UnderlineSpan() {
            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(activity.getResources().getColor(R.color.color_main));
                ds.setUnderlineText(false);
            }
        }, stringBuilder.toString().indexOf(privacyTitle2), stringBuilder.toString().indexOf(privacyTitle2) + privacyTitle2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        tvContent.setText(stringBuilder);
        tvContent.setMovementMethod(LinkMovementMethod.getInstance());

        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

        tvYes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                loginDialog.loginDialog();
            }
        });
        dialog.show();
        return dialog;
    }
    public interface LoginDialog {
        void loginDialog();
    }

    /**
     * 设置 dialog的高度
     * 可根据list的条数来设置高度
     * @param dialog
     */
//    private void setDialogHeight(List list, AlertDialog dialog) {
//        WindowManager m = getWindowManager();
//        Display display = m.getDefaultDisplay();
//        WindowManager.LayoutParams p = dialog.getWindow().getAttributes();  //获取对话框当前的参数值
//        if (list.size() > 5) p.height = (int) (display.getHeight() * 0.5);//设置为当前屏幕高度的一半
//        if (list.size() <= 5) p.height = RecyclerView.LayoutParams.WRAP_CONTENT;
//        dialog.getWindow().setAttributes(p);     //设置生效
//    }


}
